import {
	ACCESS_TOKEN,
	USERINFO
} from '@/store/mutation-types'
import storage from '@/utils/storage'
import * as LoginApi from '@/api/login'
// 登陆成功后执行
const loginSuccess = (commit, {
	access_token,
	userId
}) => {
	// 过期时间30天
	const expiryTime = 30 * 86400
	// 保存tokne
	storage.set(ACCESS_TOKEN, access_token, expiryTime)
	// 记录到store全局变量
	commit('SET_TOKEN', access_token)
}

const user = {
	state: {
		// 用户认证token
		token: '',
	},

	mutations: {
		SET_TOKEN: (state, value) => {
			state.token = value
		},
		SET_USER_ID: (state, value) => {
			state.userId = value
		},
		SET_USERINFO: (state, value) => {
			state.userInfo = value
		},
	},

	actions: {

		// 用户登录(普通登录: 输入手机号和验证码)
		loginByCode({
			commit
		}, data) {
			console.log(data);
			return new Promise((resolve, reject) => {
				LoginApi.loginByCode(data)
					.then(response => {
						console.log(response);
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},
		// 账号密码登录（手机号、密码）
		LoginPassword({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.loginPassword(data)
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},
		// 注册/找回密码登录（手机号、密码，验证码）
		RegisterPassword({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.registerPassword(data)
					.then(response => {
						const result = response.data
						// loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},
		// 微信小程序一键授权登录(获取用户基本信息)
		LoginMpWx({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.loginMpWx({
						form: data
					}, {
						isPrompt: false
					})
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 微信小程序一键授权登录(授权手机号)
		LoginMpWxMobile({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.loginMpWxMobile({
						form: data
					}, {
						isPrompt: false
					})
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 获取用户信息
		GetUserInfo({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				UserApi.info()
					.then(response => {
						const result = response.data.userInfo
						// 过期时间30天
						const expiryTime = 30 * 86400
						// 保存tokne和userId到缓存
						storage.set(USERINFO, result, expiryTime)
						// commit('SET_USERINFO',result)
						resolve(result)
					})
					.catch(reject)
			})
		},

		authLogin(store) { // 微信授权登陆
			console.log(1111111111111);
			// #ifdef MP-WEIXIN
			uni.reLaunch({
				url: "/pages/login/index"
			})
			// #endif
		},

		// 退出登录
		Logout({
			commit
		}, data) {
			const store = this
			return new Promise((resolve, reject) => {
				if (store.getters.token) {
					// 删除缓存中的tokne和userId
					storage.remove(ACCESS_TOKEN)
					storage.remove(USERINFO)
					// 记录到store全局变量
					commit('SET_TOKEN', '')
					commit('SET_USERINFO', null)
					resolve()
				}
			})
		}

	}
}

export default user